package com.haople.test.trace.document;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @author hp
 * 通过httpclient 抓取页面
 *
 */
public class HttpClientDocumentParserImpl implements DocumentParser{
	private static final String osName = System.getProperty("os.name").toLowerCase();
	public static final String DEFAULT_WINDOWS_REQUESTER_HEADER_USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8";
	public static final String DEFAULT_LINUX_REQUESTER_HEADER_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17";
	public Integer retry=5;
		public String getContent(String url){
			System.out.println("抓取url-->"+url);
			CloseableHttpClient httpClient=HttpClients.createDefault();
			try {
				RequestConfig requestConfig = RequestConfig.custom()
						.setSocketTimeout(2000)
						.setConnectTimeout(2000)
						.build();//设置请求和传输超时时间
				HttpGet httpGet=new HttpGet(url);
				httpGet.setConfig(requestConfig);
				if(osName.indexOf("window")>=0){
					httpGet.setHeader("User-Agent", DEFAULT_WINDOWS_REQUESTER_HEADER_USER_AGENT);
				}else{
					httpGet.setHeader("User-Agent", DEFAULT_LINUX_REQUESTER_HEADER_USER_AGENT);
				}
				CloseableHttpResponse httpResponse=httpClient.execute(httpGet);
				return EntityUtils.toString(httpResponse.getEntity(), "utf-8");
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				System.out.println("抓取[ "+url+" ]异常");
			} catch (SocketTimeoutException e) { //如果出现超时的异常就重试
				if(--retry>0){
					e.printStackTrace();
					System.out.println("抓取[ "+url+" ]异常--进行重试次:"+retry);
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					return getContent(url);
				}
			}catch (IOException e) {
				e.printStackTrace();
				System.out.println("抓取[ "+url+" ]异常");
			}finally{
				if(httpClient!=null){
					try {
						httpClient.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return null;
		}
}
